<?php

namespace org;

/**
 * HttpCurl Curl模拟Http工具类
 * author  eiwen
 * time 2016 12/08
 */
class HttpCurl {

    /**
     * 显示HTTP Header 信息
     * 
     * @return string
     */
    static function getHeaderInfo($header = '', $echo = true) {
        ob_start();
        $headers = getallheaders();
        if (!empty($header)) {
            $info = $headers [$header];
            echo ($header . ':' . $info . "\n");
            ;
        } else {
            foreach ($headers as $key => $val) {
                echo ("$key:$val\n");
            }
        }
        $output = ob_get_clean();
        if ($echo) {
            echo (nl2br($output));
        } else {
            return $output;
        }
    }

    /**
     * HTTP Protocol defined status codes
     * 
     * @param int $num        	
     */
    static function sendHttpStatus($code) {
        static $_status = array(
            // Informational 1xx
            100 => 'Continue',
            101 => 'Switching Protocols',
            // Success 2xx
            200 => 'OK',
            201 => 'Created',
            202 => 'Accepted',
            203 => 'Non-Authoritative Information',
            204 => 'No Content',
            205 => 'Reset Content',
            206 => 'Partial Content',
            // Redirection 3xx
            300 => 'Multiple Choices',
            301 => 'Moved Permanently',
            302 => 'Found', // 1.1
            303 => 'See Other',
            304 => 'Not Modified',
            305 => 'Use Proxy',
            // 306 is deprecated but reserved
            307 => 'Temporary Redirect',
            // Client Error 4xx
            400 => 'Bad Request',
            401 => 'Unauthorized',
            402 => 'Payment Required',
            403 => 'Forbidden',
            404 => 'Not Found',
            405 => 'Method Not Allowed',
            406 => 'Not Acceptable',
            407 => 'Proxy Authentication Required',
            408 => 'Request Timeout',
            409 => 'Conflict',
            410 => 'Gone',
            411 => 'Length Required',
            412 => 'Precondition Failed',
            413 => 'Request Entity Too Large',
            414 => 'Request-URI Too Long',
            415 => 'Unsupported Media Type',
            416 => 'Requested Range Not Satisfiable',
            417 => 'Expectation Failed',
            // Server Error 5xx
            500 => 'Internal Server Error',
            501 => 'Not Implemented',
            502 => 'Bad Gateway',
            503 => 'Service Unavailable',
            504 => 'Gateway Timeout',
            505 => 'HTTP Version Not Supported',
            509 => 'Bandwidth Limit Exceeded'
        );
        if (isset($_status [$code])) {
            header('HTTP/1.1 ' . $code . ' ' . $_status [$code]);
        }
    }

    public static function get($url, $params, $method = 'GET', $header = array(), $multi = false) {
        return self::curl($url, $params, $method, $header, $multi);
    }

    public static function post($url, $params, $method = 'POST', $header = array(), $multi = false) {
        return self::curl($url, $params, $method, $header, $multi);
    }

    /**
     * curl封装函数
     */
    public static function curl($url, $params, $method = 'GET', $header = array(), $multi = false) {
        $opts = array(
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_ENCODING => 'gzip'
        );
        /* 根据请求类型设置特定参数 */
        switch (strtoupper($method)) {
            case 'GET' :
                $opts [CURLOPT_URL] = $url . '?' . http_build_query($params);
                break;
            case 'POST' :
                // 判断是否传输文件
                $params = $multi ? $params : http_build_query($params);
                $opts [CURLOPT_URL] = $url;
                $opts [CURLOPT_POST] = 1;
                $opts [CURLOPT_POSTFIELDS] = $params;
                break;
            default :
                return 0;
        }
        /* 初始化并执行curl请求 */
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data = curl_exec($ch);
        $error = curl_error($ch);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $return_array ['FILE'] = $data;
        $return_array ['STATUS'] = curl_getinfo($ch);
        $return_array ['ERROR'] = curl_error($ch);
        $message ['result'] = json_decode($data, true);
        $return_array ['INFO'] = $message;
        $return_array ['POST'] = $params;
        curl_close($ch);
        if ($return_array ['STATUS'] ['http_code'] != 200) {
            //Log::Is_logs ( $return_array, 'error_' . $return_array ['STATUS'] ['http_code'] );
        } else {
            //Log::Is_logs ( $return_array, 'result_' . $return_array ['STATUS'] ['http_code'] );
        }
        return $data;
    }
}
